from django.core.paginator import Paginator, PageNotAnInteger, InvalidPage, EmptyPage
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views import View

from boxuegu import settings
from courses.models import Course
from operation.models import UserCourse
from organization.models import Teacher, CourseOrg


# 授课学校展示页面
class Orglist(View):
    def get(self, request):
        user_id = request.user.id
        MEDIA_URL = settings.MEDIA_URL
        usercourse = UserCourse.objects.filter(user_id=user_id)
        course = []
        all_citys = []
        for value in usercourse:
            course.append({
                'course_id': value.course_id
            })
        for key in course:
            course_q = Course.objects.get(id=key.get('course_id'))
            courseorg = CourseOrg.objects.get(id=course_q.course_org_id)
            data = {
                'id': courseorg.id,
                'name': courseorg.name
            }
            all_citys.append(data)
        course_list = Course.objects.get_queryset().order_by('id')
        paginator = Paginator(course_list, 2)
        page_id = 1
        try:
            all_orgs = paginator.page(page_id)
        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            all_orgs = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
            return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            all_orgs = paginator.page(paginator.num_pages)
        sorted_teachers = CourseOrg.objects.all().order_by('-fav_nums')[:2]
        hot_orgs = []
        for value in sorted_teachers:
            hot_orgs.append({
                'name': value.name,
                'address': value.address
            })
        return render(request, 'org-list.html',
                      {'all_citys': all_citys, 'all_orgs': all_orgs, 'hot_orgs': hot_orgs, 'MEDIA_URL': MEDIA_URL})


# 授课学校分页展示
class OrgPagelist(View):
    def get(self, request, org_id):
        user_id = request.user.id
        MEDIA_URL = settings.MEDIA_URL
        usercourse = UserCourse.objects.filter(user_id=user_id)
        course = []
        all_citys = []
        for value in usercourse:
            course.append({
                'course_id': value.course_id
            })
        for key in course:
            course_q = Course.objects.get(id=key.get('course_id'))
            courseorg = CourseOrg.objects.get(id=course_q.course_org_id)
            data = {
                'id': courseorg.id,
                'name': courseorg.name
            }
            all_citys.append(data)
        course_list = Course.objects.get_queryset().order_by('id')
        paginator = Paginator(course_list, 2)

        try:
            all_orgs = paginator.page(org_id)
        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            all_orgs = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
            return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            all_orgs = paginator.page(paginator.num_pages)

        sorted_teachers = CourseOrg.objects.all().order_by('-fav_nums')[:2]
        hot_orgs = []
        for value in sorted_teachers:
            hot_orgs.append({
                'name': value.name,
                'address': value.address
            })
        return render(request, 'org-list.html',
                      {'all_citys': all_citys, 'all_orgs': all_orgs, 'MEDIA_URL': MEDIA_URL, 'hot_orgs': hot_orgs})


# 授课教师页面展示
class Teacherlist(View):
    def get(self, request):
        teachers = Teacher.objects.get_queryset().order_by('id')
        MEDIA_URL = settings.MEDIA_URL
        user = request.user.id
        if user is None:
            user_id = 0
        else:
            user_id = user
        page = request.GET.get('page')
        paginator = Paginator(teachers, 2)
        num_pages = paginator.num_pages
        try:
            all_teachers = paginator.page(page)

        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            all_teachers = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
            return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            all_teachers = paginator.page(paginator.num_pages)
            # Currency.objects.filter(consume_total__gt=consume).order_by('consume_total')[:5]
        sorted_teachers = Teacher.objects.all().order_by('-fav_nums')[:2]
        sorted_teacher = []
        for value in sorted_teachers:
            sorted_teacher.append({
                'image': value.image,
                'name': value.name,
                'work_years': value.work_years
            })
        return render(request, 'teachers-list.html',
                      {'all_teachers': all_teachers, 'MEDIA_URL': MEDIA_URL, 'sorted_teacher': sorted_teacher})


# 授课教师分页展示
class Teacherdetail(View):
    def get(self, request, teacher_id):
        teachers = Teacher.objects.get_queryset().order_by('id')
        MEDIA_URL = settings.MEDIA_URL
        user = request.user.id
        if user is None:
            user_id = 0
        else:
            user_id = user
        page = request.GET.get('page')
        paginator = Paginator(teachers, 2)
        num_pages = paginator.num_pages
        try:
            all_teachers = paginator.page(teacher_id)

        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            all_teachers = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
            return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            all_teachers = paginator.page(paginator.num_pages)
        sorted_teachers = Teacher.objects.all().order_by('-fav_nums')[:2]
        sorted_teacher = []
        for value in sorted_teachers:
            sorted_teacher.append({
                'image': value.image,
                'name': value.name,
                'work_years': value.work_years
            })
        return render(request, 'teachers-list.html',
                      {'all_teachers': all_teachers, 'MEDIA_URL': MEDIA_URL, 'sorted_teacher': sorted_teacher})


class Orghome(View):
    def get(self, request, org_id):
        MEDIA_URL = settings.MEDIA_URL
        course = Course.objects.get(id=org_id)
        all_courses = []
        courseorg = CourseOrg.objects.get(id=course.course_org_id)
        data = {
            'id': course.id,
            'name': course.name,
            'learn_times': course.learn_times,
            'students': course.students,
            'fav_nums': course.fav_nums,
            'course_org_name': courseorg.name,
            'image': course.image
        }
        all_courses.append(data)
        all_teachers = []
        teachers = Teacher.objects.get(id=course.teacher_id)
        teacher = {
            'image': teachers.image,
            'name': teachers.name,
            'id': teachers.id

        }
        all_teachers.append(teacher)
        return render(request, 'org-detail-homepage.html',
                      {'all_courses': all_courses, 'all_teachers': all_teachers, 'course_org': courseorg,
                       'MEDIA_URL': MEDIA_URL})


class Addfav(View):
    pass


# 授课教师页面展示
class Teacherlistdetail(View):
    def get(self, request, teacher_id):
        MEDIA_URL = settings.MEDIA_URL
        teachers_id = Course.objects.all()
        teachers = Teacher.objects.get(id=teacher_id)
        city_name = CourseOrg.objects.get(id=teachers.org_id)
        teacher = {
            'image': teachers.image,
            'work_years': teachers.work_years,
            'work_company': teachers.work_company,
            'work_position': teachers.work_position,
            'points': teachers.points,
            'org_image': city_name.image,
            'address': city_name.address,
            'org_name': city_name.name
        }

        all_courses = []
        for coures in teachers_id:
            all_courses.append({
                'id': coures.id,
                'image': coures.image,
                'name': coures.name,
                'learn_times': coures.learn_times,
                'students': coures.students,
                'fav_nums': coures.fav_nums,
                'course_org_name': city_name.name

            })

        sorted_teachers = Teacher.objects.all().order_by('-fav_nums')[:2]
        sorted_teacher = []
        for value in sorted_teachers:
            sorted_teacher.append({
                'id': value.id,
                'image': value.image,
                'name': value.name,
                'work_years': value.work_years
            })

        return render(request, 'teacher-detail.html',
                      {'teacher': teacher, 'all_courses': all_courses, 'MEDIA_URL': MEDIA_URL,
                       'sorted_teacher': sorted_teacher})
